package com.gy.smart.handle;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.gy.smart.R;
import com.gy.smart.base.BaseFragment;
import com.gy.smart.handle.adapter.HandleDetailFileAdapter;
import com.gy.smart.handle.bean.HandleDetailBean;
import com.gy.smart.handle.util.HandleDetailManager;
import com.gy.smart.util.AndroidFileUtil;
import com.gy.smart.view.MyFullListView;
import com.nostra13.universalimageloader.utils.StorageUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 高岳 on 2017/1/5.
 * Describe:详情申报材料
 */
public class HandleApplyFragment extends BaseFragment implements HandleDetailManager.IHandleDetailListener {

    private TextView tvContent;
    private MyFullListView listView;
    private HandleDetailFileAdapter fileAdapter;
    private List<String> files;
    private String path;
    private HandleDetailBean detailBean;

    @Override
    protected View setView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_handle_apply,null);
        hidenTop();
        return view;
    }

    @Override
    protected void initView() {
        tvContent = (TextView) _rootView.findViewById(R.id.fg_handle_apply_tv_content);
        listView = (MyFullListView) _rootView.findViewById(R.id.fg_handle_list_down);
    }

    @Override
    protected void initData() {
        files = new ArrayList<>();
        fileAdapter = new HandleDetailFileAdapter(mContext,files);
        path = StorageUtils.getOwnCacheDirectory(mContext.getApplicationContext(), "smart").getAbsolutePath();
    }

    @Override
    protected void setListener() {
        super.setListener();
        HandleDetailManager.getInstance().addListener(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                downFile(detailBean.approval.download.get(position).down_url,detailBean.approval.download.get(position).down_name);
            }
        });
    }

    @Override
    protected void fillData() {
        super.fillData();
        listView.setAdapter(fileAdapter);
    }

    private void downFile(String url, String name) {
        showLoadDialog("下载中...");
        RequestParams params = new RequestParams(url);
        path = path + "/" + name;
        params.setSaveFilePath(path);
        x.http().get(params, new Callback.CommonCallback<File>() {
            @Override
            public void onSuccess(File result) {
                closeLoadDialog();
                showToast("下载成功");
                Intent intent = AndroidFileUtil.openFile(result.getAbsolutePath());
                if(intent != null){
                    startActivity(intent);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeLoadDialog();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                closeLoadDialog();
            }

            @Override
            public void onFinished() {
                closeLoadDialog();
            }
        });
    }

    @Override
    public void handleDetailChange(HandleDetailBean detailBean) {
        this.detailBean = detailBean;
        tvContent.setText(detailBean.approval.approval_content);
        if(detailBean.approval != null && detailBean.approval.download != null){
            for(HandleDetailBean.ApprovalBean.DownloadBean bean : detailBean.approval.download){
                files.add(bean.down_name);
            }
            fileAdapter.notifyDataSetChanged();
        }

    }
}
